<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="12">
        <div class="member-toolbar">
          <div class="member-toolbar-course">            
            <span>{{theTeaching.cno}} | {{theTeaching.cname}} | {{theTeaching.period}}学时</span></div>
          <div class="member-toolbar-tea">
            <span>{{Math.floor(theTeaching.cterm / 10)}}年.</span>
            <span v-if="theTeaching.cterm%10==0">春</span>
            <span v-else-if="theTeaching.cterm%10==1">秋</span>
            <span>{{theTeaching.tname}}</span>
            <span>{{theTeaching.ps}}</span></div>
        </div>
        <div class="member-toolbar">
          <div class="member-toolbar-course">            
            <span>选课</span></div>
          <div class="member-toolbar-tea">
            <span v-if="theTeaching.isOpen" style="color:#67C23A;font-weight:bold">开启</span>
            <span v-else style="color:#DCDFE6">关闭</span>
          </div>
        </div>
        <div class="member-toolbar">
          <div class="member-toolbar-course">            
            <span>录分</span></div>
          <div class="member-toolbar-tea">
            <span v-if="theTeaching.isOpenScore" style="color:#67C23A;font-weight:bold">开启</span>
            <span v-else style="color:#DCDFE6">关闭</span>
          </div>
        </div>
      </el-col>
      <el-col :span="7.5">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" 
        label-width="68px">
          <el-form-item label="学生姓名" prop="cname">
            <el-input
              v-model="queryParams.sname"
              placeholder="请输入学生姓名"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-download" size="mini" @click="handleSc" v-hasPermi="['jxgl:sc:by']">代填选课</el-button>      
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['jxgl:sc:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学号" align="center" prop="sno" />
      <el-table-column label="姓名" align="center" prop="sname" />
      <el-table-column label="班级" align="center" prop="sclass" />
      <el-table-column label="性别" align="center" prop="ssex" />
      <el-table-column label="分数" align="center" prop="score" />
      <el-table-column label="绩点" align="center" prop="point" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" :disabled="!theTeaching.isOpenScore"
            @click="handleMembersc(scope.row)" v-hasPermi="['jxgl:sc:edit']" >编辑成绩</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 编辑学生成绩对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学期" prop="cterm">
          <span>{{Math.floor(theTeaching.cterm / 10)}}年.</span>
          <span v-if="theTeaching.cterm%10==0">春</span>
          <span v-else-if="theTeaching.cterm%10==1">秋</span>
        </el-form-item>
        <el-form-item label="课程" prop="cname">
          <span>{{theTeaching.cno}} | {{theTeaching.cname}} | {{theTeaching.period}}学时</span>
        </el-form-item>
        <el-form-item label="成绩" prop="score">
          <el-input v-model="form.score" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="绩点" prop="point">
          <el-input v-model="form.point" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
  .member-toolbar{
    margin-left: 20px;
    display:inline-block;
  }
  .member-toolbar-course{
    font-size: 14px;
    font-weight: bold;
  }
  .member-toolbar-tea{
    font-size: 12px;
    margin-top:5px
  }
  .member-toolbar-tea span{
    margin-right: 5px;
  }
</style>

<script>
import { listScMember,updateSc} from "@/api/jxgl/sc";
import { getTeaching} from "@/api/jxgl/teaching";

export default {
  name: "Student",
  dicts: ['sys_user_sex'],
  data() {
    return {
      disabledFlag:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生档案表格数据
      stuList: [],
      theTeaching:{
        cno:"1001",
        cname:"测试",
        cterm:20240,
        tname:"张三",
        ps:"",
        isOpen:0,
        isOpenScore:0
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sname: null,
        phone: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sname: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTheTeaching();
  },
  methods: {
    /** 查询学生档案列表 */
    getList() {
      this.loading = true;
      listScMember(this.$route.params.tid,this.queryParams).then(response => {
        this.stuList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTheTeaching() {
      this.loading = true;
      //this.$route代码当前路由，params代表传入的参数
      getTeaching(this.$route.params.tid).then(response => {
        this.theTeaching = response.data;
        console.log(this.theTeaching)
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sno: null,
        tid: null,
        cno: null,
        ssex: null,
        score:null,
        point:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sno)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改成员成绩操作 */
    handleMembersc(row) {
      this.open = true;
      this.form=row;
      console.log(this.form)
      this.title = "编辑成绩";
    },
    /** 处理代填选课 */
    handleSc(){

    },
    /** 导出按钮操作 */
    handleExport() {

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          var obj = {
            sno: this.form.sno,
            tid: this.form.tid,
            cno: this.form.cno,
            score: this.form.score,
            point: this.form.point
          };
          console.log(obj)
          updateSc(obj).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }else{
          alert("验证不通过！")
        }
      });
    },
  }

};
</script>
